﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MyMoviesBrowserLibrary;
using System.Windows.Threading;

namespace TVManager
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private TVManagerBusinessLayer bl;
        private Queue<Series> queue;
        public Window1()
        {
            InitializeComponent();
            if (Settings.Default == null || string.IsNullOrEmpty(Settings.Default.MyMoviesServerName))
            {
                MessageBox.Show("The server name has not been set, please run the configuration tool.");
                Application.Current.Shutdown();                
                return;
            }


            bl = new TVManagerBusinessLayer();
            bl.AddSeriesCompleted += new AddSeriesEvent(bl_AddSeriesCompleted);
            bl.DataUpdated += new UpdateEvent(bl_DataUpdated);

            queue = new Queue<Series>();
            importFolderButton.Click += new RoutedEventHandler(importFolderButton_Click);

            seriesListBox.SelectionChanged += new SelectionChangedEventHandler(seriesListBox_SelectionChanged);
            seriesControl.SeriesUpdated += new SeriesUpdatedEvent(seriesControl_SeriesUpdated);
            settingsMenu.Click += new RoutedEventHandler(settingsMenu_Click);
            updateSeriesButton.Click += new RoutedEventHandler(updateSeriesButton_Click);

            seriesControl.StatusUpdated += new StatusUpdatedEvent(seriesControl_StatusUpdated);

            Loaded += new RoutedEventHandler(Window1_Loaded);
        }

        void seriesControl_StatusUpdated(string statusText, bool showControl, bool isIndeterminate, int percent)
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)delegate()
            {
                statusControl.StatusText = statusText;
                if (showControl)
                    statusControl.Show();
                else
                    statusControl.Hide();
                statusControl.IsIndeterminate = isIndeterminate;
                statusControl.ProgressBarValue = percent;
            });
        }

        void bl_DataUpdated(string updateText)
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)delegate()
            {
                statusControl.StatusText = updateText;
            });
        }

        void bl_AddSeriesCompleted(Series series)
        {
            ProcessQueue();
        }

        void updateSeriesButton_Click(object sender, RoutedEventArgs e)
        {
            statusControl.Show();
            foreach (Series series in seriesListBox.SelectedItems.Cast<Series>())
                queue.Enqueue(series);
            ProcessQueue();
        }

        void ProcessQueue()
        {
            if (queue.Count > 0)
            {
                Series series = queue.Dequeue();
                bl.AddSeriesAsync(series.SeriesId, series.FolderLocation);
            }
            else
            {
                statusControl.Hide();
            }
        }

        void settingsMenu_Click(object sender, RoutedEventArgs e)
        {
            SettingsWindow settingsWindow = new SettingsWindow();
            settingsWindow.ShowDialog();
        }

        void seriesControl_SeriesUpdated(Series series)
        {
            if (series == null)
                return;
            LoadSeries();
            List<Series> lSeries = seriesListBox.ItemsSource as List<Series>;
            Series tseries = lSeries.SingleOrDefault(s => s.SeriesId == series.SeriesId);
            if (tseries != null)
            {
                seriesListBox.SelectedItem = tseries;
            }

        }

        void seriesListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            updateSeriesButton.Visibility = Visibility.Collapsed;
            if (seriesListBox.SelectedItems.Count == 1)
            {                
                Series series = seriesListBox.SelectedValue as Series;
                seriesControl.Series = series;
            }
            else if (seriesListBox.SelectedItems.Count > 1)
            {
                updateSeriesButton.Visibility = Visibility.Visible;
            }
        }

        void importFolderButton_Click(object sender, RoutedEventArgs e)
        {
            ImportFolderWindow importFolderWindow = new ImportFolderWindow();
            importFolderWindow.ShowDialog();
            LoadSeries();
        }

        void Window1_Loaded(object sender, RoutedEventArgs e)
        {
            
            if (string.IsNullOrEmpty(bl.ChoosenLanguage))
            {
                SettingsWindow settingsWindow = new SettingsWindow();
                settingsWindow.ShowDialog();
            }

            LoadSeries();
        }



        void LoadSeries()
        {
            seriesListBox.ItemsSource = null;
            seriesListBox.ItemsSource = bl.GetSeries();
        }
    }
}
